package game;

import java.awt.event.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GrandeJava extends JFrame implements WindowListener, ActionListener, ItemListener {

	private static final long serialVersionUID = 1L; //serial version
	private static GrandeCanvas canvas;
	protected java.awt.Menu sub;
	protected java.awt.Menu type;
	public GrandeJava(GrandeCanvas g){
		super("Game");
		pack();
		setResizable(true);
		try {
			this.setIconImage(ImageIO.read(new File("images/question_block.gif")));
		} catch (IOException e) {
		}
		Container c = getContentPane();
		canvas = g;
		c.add(canvas);
		pack();
		initMenu();
		addWindowListener(this);
		setResizable(true);
		setVisible(true);
	}
	
	public void initMenu(){
		MenuBar bar = new MenuBar();
		MenuItemGroup pro = new MenuItemGroup();
		java.awt.Menu a = new java.awt.Menu("File");
		Checkbox prop = new Checkbox("Properties", pro);
		a.add("Save");
		a.add("Save As...");
		a.add("Open...");
		a.add("New");
		a.add(prop);
		prop.addItemListener(this);
		a.addActionListener(this);
		bar.add(a);
		
		java.awt.Menu b = new java.awt.Menu("Mode");
		MenuItemGroup types = new MenuItemGroup();
		Checkbox draw = new Checkbox("Draw", types);
		Checkbox remove = new Checkbox("Remove", types);
		Checkbox select = new Checkbox("Select", types);
		Checkbox none = new Checkbox("None", types);
		types.selectItem(none);
		draw.addItemListener(this);
		remove.addItemListener(this);
		select.addItemListener(this);
		none.addItemListener(this);
		b.add(draw);
		b.add(remove);
		b.add(select);
		b.add(none);
		bar.add(b);
		
		java.awt.Menu c = new java.awt.Menu("Draw");
		MenuItemGroup group = new MenuItemGroup();
		Checkbox plat = new Checkbox("Normal", group);
		Checkbox elevator = new Checkbox("Elevator", group);
		Checkbox movePlat = new Checkbox("Moving Platform", group);
		type = new java.awt.Menu("Icon");
		Checkbox brick = new Checkbox("Brick", group);
		Checkbox qus = new Checkbox("Question", group);
		
		type.add(brick);
		type.add(qus);
		
		brick.addItemListener(this);
		qus.addItemListener(this);
		
		Checkbox door = new Checkbox("Door", group);
		
		Checkbox friction = new Checkbox("Friction", group);
		Checkbox grav = new Checkbox("Gravity", group);
		Checkbox speed = new Checkbox("Speed", group);
		Checkbox booster = new Checkbox("Booster", group);
		Checkbox button = new Checkbox("Button", group);
		Checkbox tele = new Checkbox("Teleporter", group);
		Checkbox spike = new Checkbox("Spike", group);
		Checkbox spawn = new Checkbox("Spawn", group);
		sub = new java.awt.Menu("Enemy");
		Checkbox norm = new Checkbox("Normal Enemy", group);
		Checkbox ice = new Checkbox("Ice Enemy", group);
		Checkbox fire = new Checkbox("Fire Enemy", group);
		
		sub.add(norm);
		sub.add(ice);
		sub.add(fire);
		
		norm.addItemListener(this);
		ice.addItemListener(this);
		fire.addItemListener(this);
		
		plat.addItemListener(this);
		elevator.addItemListener(this);
		movePlat.addItemListener(this);
		door.addItemListener(this);
		
		friction.addItemListener(this);
		grav.addItemListener(this);
		speed.addItemListener(this);
		booster.addItemListener(this);
		button.addItemListener(this);
		tele.addItemListener(this);
		spike.addItemListener(this);
		spawn.addItemListener(this);

		c.add("Platforms");
		c.getItem(c.getItemCount()-1).setEnabled(false);
		c.addSeparator();
		
		c.add(plat);
		c.add(elevator);
		c.add(movePlat);
		c.add(type);
		c.add(door);
		
		c.addSeparator();
		c.add("Fields");
		c.getItem(c.getItemCount()-1).setEnabled(false);
		c.addSeparator();
		
		c.add(friction);
		c.add(grav);
		c.add(speed);
		c.add(booster);
		c.add(button);
		c.add(tele);
		c.add(spike);
		c.add(spawn);
		c.add(sub);
		
		bar.add(c);
		
		java.awt.Menu e = new java.awt.Menu("Colors");
		MenuItemGroup colors = new MenuItemGroup();
		Checkbox red = new Checkbox("Red", colors);
		Checkbox blue = new Checkbox("Blue", colors);
		Checkbox green = new Checkbox("Green", colors);
		Checkbox yellow = new Checkbox("Yellow", colors);
		Checkbox brown = new Checkbox("Brown", colors);
		Checkbox black = new Checkbox("Black", colors);
		red.addItemListener(this);
		blue.addItemListener(this);
		green.addItemListener(this);
		yellow.addItemListener(this);
		brown.addItemListener(this);
		black.addItemListener(this);
		e.add(red);
		e.add(blue);
		e.add(green);
		e.add(yellow);
		e.add(brown);
		e.add(black);
		bar.add(e);
		
		java.awt.Menu f = new java.awt.Menu("Link");
		f.add("Teleporters");
		f.add("Button/Door");
		f.addActionListener(this);
		bar.add(f);
		
		setMenuBar(bar);
	}
	
	public void itemStateChanged(ItemEvent e){
		if(e.getSource() instanceof Checkbox && e.getStateChange() == ItemEvent.SELECTED){
			Checkbox c = (Checkbox)e.getSource();
			c.getGroup().selectItem((Checkbox)e.getSource());
			if(c.getActionCommand().equals("Red")){
				canvas.drawColor = Color.red;
			}else if(c.getActionCommand().equals("Blue")){
				canvas.drawColor = Color.blue;
			}else if(c.getActionCommand().equals("Green")){
				canvas.drawColor = Color.green.darker().darker();
			}else if(c.getActionCommand().equals("Yellow")){
				canvas.drawColor = Color.yellow.darker();
			}else if(c.getActionCommand().equals("Brown")){
				canvas.drawColor = new Color(100, 50, 0);
			}else if(c.getActionCommand().equals("Black")){
				canvas.drawColor = Color.black;
			}else if(c.getActionCommand().equals("Normal")){
				canvas.drawPlatType = 1;
			}else if(c.getActionCommand().equals("Elevator")){
				canvas.drawVars.set(0, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move height:  ", "Move Height", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawVars.set(1, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move speed:  ", "Move Speed", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 3;
			}else if(c.getActionCommand().equals("Moving Platform")){
				canvas.drawVars.set(0, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move width:  ", "Move Width", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawVars.set(1, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move speed:  ", "Move Speed", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 2;
			}else if(c.getActionCommand().equals("Brick")){
				canvas.drawPlatType = 6;
			}else if(c.getActionCommand().equals("Question")){
				canvas.drawPlatType = 14;
			}else if(c.getActionCommand().equals("Door")){
				canvas.drawVars.set(0, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move x:  ", "Move Width", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawVars.set(1, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter move y:  ", "Move Speed", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 9;
			}else if(c.getActionCommand().equals("Friction")){
				canvas.drawVars.set(2, java.lang.Float.parseFloat(JOptionPane.showInputDialog(canvas, "Enter friction:  ", "Friction", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 4;
			}else if(c.getActionCommand().equals("Gravity")){
				canvas.drawVars.set(2, java.lang.Float.parseFloat(JOptionPane.showInputDialog(canvas, "Enter gravity:  ", "Gravity", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 5;
			}else if(c.getActionCommand().equals("Speed")){
				canvas.drawVars.set(0, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter speed:  ", "Speed", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 7;
			}else if(c.getActionCommand().equals("Booster")){
				canvas.drawVars.set(0, Integer.parseInt(JOptionPane.showInputDialog(canvas, "Enter boost:  ", "Booster", JOptionPane.PLAIN_MESSAGE)));
				canvas.drawPlatType = 8;
			}else if(c.getActionCommand().equals("Button")){
				canvas.drawPlatType = 10;
			}else if(c.getActionCommand().equals("Teleporter")){
				canvas.drawPlatType = 11;
			}
			else if(c.getActionCommand().equals("Spike")){
				canvas.drawPlatType = 12;
			}else if(c.getActionCommand().equals("Normal Enemy")){
				canvas.drawPlatType = 13;
			}else if(c.getActionCommand().equals("Ice Enemy")){
				canvas.drawPlatType = 13;
			}else if(c.getActionCommand().equals("Fire Enemy")){
				canvas.drawPlatType = 13;
			}else if(c.getActionCommand().equals("Draw")){
				canvas.drawOn = true;
				canvas.removeOn = false;
				canvas.selectOn = false;
				canvas.selected = null;
				canvas.selected2 = null;
			}else if(c.getActionCommand().equals("Remove")){
				canvas.removeOn = true;
				canvas.drawOn = false;
				canvas.drawing = false;
				canvas.selectOn = false;
				canvas.selected = null;
				canvas.selected2 = null;
			}else if(c.getActionCommand().equals("Select")){
				canvas.removeOn = false;
				canvas.drawOn = false;
				canvas.drawing = false;
				canvas.selectOn = true;
			}else if(c.getActionCommand().equals("None")){
				canvas.removeOn = false;
				canvas.drawOn = false;
				canvas.drawing = false;
				canvas.selectOn = false;
				canvas.selected = null;
				canvas.selected2 = null;
			}else if(c.getActionCommand().equals("Spawn")){
				canvas.drawPlatType = 15;
			}else if(c.getActionCommand().equals("Properties")){
				canvas.level.disProp = true;
			}
		}
	}
	
	public void actionPerformed(ActionEvent e){
		String s = e.getActionCommand();
		if(s.equals("Save")){
			canvas.level.save();
			JOptionPane.showMessageDialog(canvas, "Level Saved");
		}else if(s.equals("Save As...")){
			JFileChooser window = new JFileChooser(new File("levels"));
			LvlFileFilter filter = new LvlFileFilter();
	        window.addChoosableFileFilter(filter);
	        window.setFileFilter(filter);
	        window.setAcceptAllFileFilterUsed(false);
			int r = window.showSaveDialog(null);
			try {
				if (r == JFileChooser.APPROVE_OPTION) {
					File q = window.getSelectedFile();
					File file = new File(q.toString());
					canvas.level.saveAs(q + ".lvl");
					if(file.createNewFile()== true){
						JOptionPane.showMessageDialog(window,"Level Created");
					}else{
						JOptionPane.showMessageDialog(window, "Level Overwritten");
					}
				}
			}catch (IOException e1) {}
		}else if(s.equals("New")){
			String name = JOptionPane.showInputDialog(canvas, "Enter file name:  ", "New Level", JOptionPane.PLAIN_MESSAGE);
			if(name != null){
				File q = new File("levels/"+name+".lvl");
				Level l = new Level(canvas, q);
				canvas.level = l;
			}
		}else if(s.equals("Open...")){
			JFileChooser fc = new JFileChooser(new File("levels"));
			LvlFileFilter filter = new LvlFileFilter();
            fc.addChoosableFileFilter(filter);
            fc.setFileFilter(filter);
            int val = fc.showOpenDialog(null);
            if(val == fc.APPROVE_OPTION && filter.accept(fc.getSelectedFile())){
                Level lvl = new Level(canvas, fc.getSelectedFile());
                canvas.level = lvl;
                canvas.level.read();
                JOptionPane.showMessageDialog(canvas, "Level Loaded");
                canvas.resumeGame();
            }  
		}else if(e.getActionCommand().equals("Teleporters")){
			if(canvas.selected != null && canvas.selected2 != null){
				if(canvas.selected instanceof Teleporter && canvas.selected2 instanceof Teleporter){
					((Teleporter)canvas.selected).setTeleporter(((Teleporter)canvas.selected2));
					((Teleporter)canvas.selected2).setTeleporter(((Teleporter)canvas.selected));
					JOptionPane.showMessageDialog(this, "Link Sucessful", "Sucess", JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(this, "ERROR: Invalid Platforms", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}else{
				JOptionPane.showMessageDialog(this, "ERROR: Not enough platforms selected", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}else if(e.getActionCommand().equals("Button/Door")){
			if(canvas.selected != null && canvas.selected2 != null){
				if(canvas.selected instanceof Button){
					if(canvas.selected2 instanceof ActionPlatform){
						((Button)canvas.selected).setActionPlatform((ActionPlatform)canvas.selected2);
						JOptionPane.showMessageDialog(this, "Link Sucessful", "Sucess", JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(this, "ERROR: Invalid action platform", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}else if(canvas.selected2 instanceof Button){
					if(canvas.selected instanceof ActionPlatform){
						((Button)canvas.selected2).setActionPlatform((ActionPlatform)canvas.selected);
						JOptionPane.showMessageDialog(this, "Link Sucessful", "Sucess", JOptionPane.INFORMATION_MESSAGE);
					}else{
						JOptionPane.showMessageDialog(this, "ERROR: Invalid action platform", "Error", JOptionPane.ERROR_MESSAGE);
					}
				}else{
					JOptionPane.showMessageDialog(this, "ERROR: No Button", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	@Override
	public void windowActivated(WindowEvent e) {
		canvas.restartGame();
	}

	@Override
	public void windowClosed(WindowEvent e) {
		canvas.quitGame();
	}

	@Override
	public void windowClosing(WindowEvent e) {
		canvas.quitGame();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		canvas.endGame();
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		canvas.restartGame();
	}

	@Override
	public void windowIconified(WindowEvent e) {
		canvas.endGame();
	}

	@Override
	public void windowOpened(WindowEvent e) {}

}
